package pl.easyshop.core.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.springframework.security.core.GrantedAuthority;

/**
 * User group data model.
 * 
 * @author Pawel Witkowski
 */
@Entity
@Table (name="TBL_USER_GROUP")
public class UserGroupModel extends AbstractBaseModel {

	/** Unique serialization identifier. */
	private static final long serialVersionUID = -541556185280854241L;

	/** Group name. */
	@Column(name="name", length=64, nullable=false)
	private String name;
	
	/** Description. */
	@Column(name="description", length=1024)
	private String description;
	
	/** Value <code>true</code> means active group. */
	@Column(name="is_enabled", nullable=false)
	private boolean enabled;

	/** Group authorities/roles. */
	@OneToMany(targetEntity=UserGroupAuthorityModel.class, mappedBy="group", cascade=CascadeType.ALL)
	@LazyCollection(LazyCollectionOption.FALSE)
	private Set<UserGroupAuthorityModel> roles;
	
	/**Group members. */
	@ManyToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE})
	@LazyCollection(LazyCollectionOption.TRUE)
	private List<UserModel> members = new ArrayList<UserModel>();

	/**
	 * Add user to group.
	 * 
	 * @param user user model.
	 */
	public void addToGroup(UserModel user) {
		members.add(user);
		user.getGroups().add(this);
	}

	/**
	 * Remove user from group.
	 * 
	 * @param user user to remove.
	 */
	public void removeFromGroup(UserModel user) {
		members.remove(user);
		user.getGroups().remove(user);
	}
	
	/**
	 * Add role to group.
	 * 
	 * @param role role.
	 */
	public void addRole(UserGroupAuthorityModel role) {
		roles.add(role);
		role.setGroup(this);
	}
	/**
	 * Get name property value.
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Set name property value.
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Get description property value.
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Set description property value.
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * Get enabled property value.
	 * @return the enabled
	 */
	public boolean isEnabled() {
		return enabled;
	}

	/**
	 * Set enabled property value.
	 * @param enabled the enabled to set
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	/**
	 * Get roles property value.
	 * @return the roles
	 */
	public Set<UserGroupAuthorityModel> getRoles() {
		return roles;
	}

	/**
	 * Set roles property value.
	 * @param roles the roles to set
	 */
	public void setRoles(Set<UserGroupAuthorityModel> roles) {
		this.roles = roles;
	}

	/**
	 * Get members property value.
	 * @return the members
	 */
	public List<UserModel> getMembers() {
		return members;
	}

	/**
	 * Set members property value.
	 * @param members the members to set
	 */
	public void setMembers(List<UserModel> members) {
		this.members = members;
	}
}
